import { post, postJson } from './utils';
import axios from 'axios';
export const gethl = (variable = 'hl') => {
  var arrStr = window.location.search.substring(1).split("&");
  for (var i = 0; i < arrStr.length; i++) {
    var temp = arrStr[i].split("=");
    if (temp[0] === variable){
        return decodeURIComponent(temp[1]);
    }    
  }
  return "zh_cn"
}
var ghl = gethl()

export function URLP(variable) {
  var arrStr = window.location.search.substring(1).split("&");
  for (var i = 0; i < arrStr.length; i++) {
    var temp = arrStr[i].split("=");
    if (temp[0] === variable){
        return decodeURIComponent(temp[1]);
    }    
  }
  return false
} 

// export const domain = "//10.10.10.1:8080/";
export const domain = "//iuu.pub/";

export function OauthBindCode(phone,itac,authtoken,hl=ghl){
  return post(domain+"api/oauth/bind/code?phone="+phone+"&itac="+itac+"&hl="+hl,authtoken)
}

export function OauthBindPhone(phone,code,itac,authtoken,hl=ghl){
  return post(domain+"api/oauth/bind/phone?phone="+phone+"&itac="+itac+"&code="+code+"&hl="+hl,authtoken)
}

export function FromStaffList(limit,offset,authtoken,hl=ghl){
  return post(domain+"api/workorder/from/staff/list?limit="+limit+"&offset="+offset+"&hl="+hl,authtoken)
}

export function MessageHandle(order_no,authtoken,hl=ghl){
  return post(domain+"api/workorder/message/handle?order_no="+order_no+"&hl="+hl,authtoken)
}

export function MessageReceive(order_no,limit,offset,authtoken,hl=ghl){
  return post(domain+"api/workorder/message/receive?order_no="+order_no+"&limit="+limit+"&offset="+offset+"&hl="+hl,authtoken)
}

export function UseCodeToToken(code,hl=ghl){
  return post(domain+"api/oauth/usecodetotoken?code="+code+"&hl="+hl)
}

export function ResetPassword(phone,pass,itac,code,hl=ghl){
  return post(domain+"api/auth/reset/password?pass="+pass+"&phone="+phone+"&hl="+hl+"&itac="+itac+"&code="+code)
}

export function GetUnderwriterFromInfo(order_no,authtoken,hl=ghl){
  return post(domain+"api/underwriter/from/info?hl="+hl+"&order_no="+order_no,authtoken)
}
export function CreateUnderwriterFrom(order_no,data,authtoken,hl=ghl){
  return postJson(domain+"api/underwriter/from?hl="+hl+"&order_no="+order_no,data,authtoken)
}
export function UnderwriterCreateOrder(authtoken,hl=ghl){
  return post(domain+"api/underwriter/create/order?hl="+hl,authtoken)
}
export function UnderwriterQueryReceiveFreecount(authtoken,hl=ghl){
  return post(domain+"api/underwriter/query/receive/freecount?hl="+hl,authtoken)
}

export function GetWorkOrderFromInfo(order_no,authtoken,hl=ghl){
  return post(domain+"api/workorder/from/info?hl="+hl+"&order_no="+order_no,authtoken)
}
export function CreateWorkOrderFrom(order_no,data,authtoken,hl=ghl){
  return postJson(domain+"api/workorder/from?hl="+hl+"&order_no="+order_no,data,authtoken)
}
export function WorkOrderCreateOrder(authtoken,hl=ghl){
  return post(domain+"api/workorder/create/order?hl="+hl,authtoken)
}
export function WorkOrderQueryReceiveFreecount(authtoken,hl=ghl){
  return post(domain+"api/workorder/query/receive/freecount?hl="+hl,authtoken)
}

export function login(phone,pass,itac,hl=ghl) {
  return post(domain+"api/auth/login?pass="+pass+"&phone="+phone+"&hl="+hl+"&itac="+itac)
}
export function StaffLogin(phone,pass,itac,hl=ghl) {
  return post(domain+"api/staff/login?pass="+pass+"&phone="+phone+"&hl="+hl+"&itac="+itac)
}
export function register(phone,pass,itac,code,hl=ghl) {
  return post(domain+"api/auth/register?pass="+pass+"&phone="+phone+"&hl="+hl+"&itac="+itac+"&code="+code)
}
export function getphonecode(phone,itac,hl=ghl,type=2) {
  return post(domain+"api/auth/send/msgcode?phone="+phone+"&hl="+hl+"&itac="+itac+"&type="+type)
  // http://www.likehealth.com.cn/apis/user/code_phone?phone=13064754732&type=1&f=1&kind=1
}
// 不必要的 authtoken
export function oauthlogin(redirect_call,authtoken,hl=ghl) {
  return post(domain+"api/oauth/wechat?redirect_call="+redirect_call+"&hl="+hl, authtoken)
}
export function paymember(authtoken,hl=ghl) {
  return post(domain+"api/member/join/order?product_id=1"+"&hl="+hl, authtoken)
}

export function wxpayjsapi(order_no, authtoken, notify_url = domain+"api/member/order/notify",hl=ghl) {
  return post(domain+'api/pay/wechat/jsapi?order_no='+ order_no + '&notify_url='+ notify_url+"&hl="+hl,authtoken)
}
// orderstatus 支付完成后检查订单状态
export function orderstatus(order_no, authtoken,hl=ghl) {
  return post(domain+'api/order/status?order_no='+ order_no+"&hl="+hl,authtoken)
}
export function memberjoincheck(authtoken,hl=ghl){
  return post(domain+'api/member/join/check'+"?hl="+hl, authtoken)
}

export function allorder(authtoken,hl=ghl){
  return post(domain+'api/order/all'+"?hl="+hl, authtoken)
}

export function nopayorder(authtoken,hl=ghl){
  return post(domain+'api/order/nopay'+"?hl="+hl, authtoken)
}

export function okorder(authtoken,hl=ghl){
  return post(domain+'api/order/ok'+"?hl="+hl, authtoken)
}

export function wxjssdk(){
    return post(domain+'api/sdk/wechat/jssdk?sdk_uri='+encodeURIComponent(window.location.href))
}
export function userinfo(authtoken,hl=ghl){
  return post(domain+'api/user/info'+"?hl="+hl, authtoken)
}
export function deleteorder(order_no,authtoken,hl=ghl){
  return post(domain+'api/order/delete?order_no='+order_no+"&hl="+hl, authtoken)
}
export function revokeorder(order_no,authtoken,hl=ghl){
  return post(domain+'api/order/revoke?order_no='+order_no+"&hl="+hl, authtoken)
}

export function getEmojiData() {
  return axios({
      method: 'get',
      url: './../static/emojiDB.json',
    })
    .then(function(res) {
      return Promise.resolve(res.data);
    });
}